package com.zmj.sy.mom.srv.aps.service;

import com.zmj.sy.mom.srv.aps.bean.entity.resource.DictData;
import com.zmj.sy.mom.srv.aps.bean.entity.resource.DictType;
import com.zmj.sy.mom.srv.aps.bean.vo.base.Result;
import com.zmj.sy.mom.srv.aps.bean.vo.nest.andon.SendCardMsgDto;
import com.zmj.sy.mom.srv.aps.feign.MomAndonFeign;
import com.zmj.sy.mom.srv.aps.mapper.DictDataMapper;
import com.zmj.sy.mom.srv.aps.mapper.DictTypeMapper;
import com.zmj.sy.mom.srv.aps.utils.JsonUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
@Service
public class MomAndonService {

    private final DictTypeMapper dictTypeMapper;
    private final DictDataMapper dictDataMapper;
    private final MomAndonFeign momAndonFeign;

    public void sendMsgFlyBook(String msg) {

        DictType personBind = dictTypeMapper.lambdaQuery().eq(DictType::getDictCode, "nestMsgPush").one();
        List<DictData> personBindList = dictDataMapper.lambdaQuery().eq(DictData::getDictType, personBind.getId()).list();
        List<String> collect = personBindList.stream().map(DictData::getDictValue).distinct().collect(Collectors.toList());
        for (String s : collect) {
            SendCardMsgDto dto = new SendCardMsgDto();
            dto.setReceive_id(s);
            dto.setTitle("套料异常");
            dto.setContent(msg);
            dto.setTemplate_type("1");
            log.info("发送到飞书的消息：{}", JsonUtils.toJsonString(dto));
            Result result = momAndonFeign.sendCardMsg(dto);
            log.info("发送到飞书的响应：{}", JsonUtils.toJsonString(result));
        }

    }

}
